const { promisify } = require("util");

const Redis = {
  async SMembers(key) {
    try {
      const members = await global.redisClient.smembers(key);
      return members;
    } catch (error) {
      console.log(error);
    }
  },
  /**
   *
   * @param {*} key
   * @param {*} value
   * @param {*} expiration  过期时间 单位秒
   */
  async Set(key, value, expiration) {
    try {
      if (expiration === 0) {
        await global.redisClient.set(key, JSON.stringify(value));
      } else {
        await global.redisClient.set(
          key,
          JSON.stringify(value),
          "EX",
          expiration
        );
      }
    } catch (error) {
      console.log("set失败", error);
    }
  },
  async GetVal(key) {
    try {
      let value = await global.redisClient.get(key);
      return value;
    } catch (error) {
      console.log("GetVal获取失败", error);
      return undefined;
    }
  },
  async Del(key) {
    try {
      const delAsync = promisify(global.redisClient.del).bind(
        global.redisClient
      );
      await delAsync(key);
    } catch (error) {
      console.log("删除redis键失败");
    }
  },
  async GetInt(key) {
    //获取数字
    try {
      let value = await global.redisClient.get(key);
      return Number(value);
    } catch (error) {
      console.log("GetInt获取失败", error);
      return undefined;
    }
  },
  /**
   *  获取[哈希表(Hash)]中 所有的字段和值
   * @param {*} key
   */
  async HGetAll(key) {
    try {
      let result = await global.redisClient.hgetall(key);
      return new Map(Object.entries(result));
    } catch (error) {
      console.log("HGetAll失败", error);
    }
  },
  async ZRangeWithScores(key, start, stop) {
    try {
      const result = await global.redisClient.zrangebyscore(
        key,
        start,
        stop,
        "WITHSCORES"
      );
      const parsedResult = [];
      for (let i = 0; i < result.length; i += 2) {
        parsedResult.push({
          member: result[i],
          score: parseFloat(result[i + 1]),
        });
      }
      return parsedResult;
    } catch (error) {
      console.log("ZRangeWithScores失败", error);
    }
  },
  async GetResult(key) {
    const result = await global.redisClient.get(key);
    return result;
  },
  //获取所有对应规则的键
  async Keys(pattern) {
    let keys = await global.redisClient.keys(pattern);
    return keys;
  },
  //在指定字段的整数上增量
  async ZincrBy(key, member, incr) {
    try {
      await global.redisClient.zincrby(key, incr, member);
    } catch (error) {
      console.log("zincrby失败", error);
    }
  },
  async ZScore(key, member) {
    const score = await global.redisClient.zscore(key, member);
    return score ? parseInt(score) : 0;
  },
  async HGet(key, field) {
    const val = await global.redisClient.hget(key, field);
    return val ? parseInt(val) : 0;
  },
  async SIsMember(key, member) {
    try {
      const result = await global.redisClient.sismember(key, member);
      return !!result;
    } catch (error) {
      console.log("SIsMember失败", error);
    }
  },
  async SRem(key, member) {
    try {
      await global.redisClient.srem(key, member);
    } catch (error) {
      console.log("SRem失败", error);
    }
  },
  async HIncrBy(key, field, incr) {
    try {
      await global.redisClient.hincrby(key, field, incr);
    } catch (error) {
      console.log("HIncrBy失败", error);
    }
  },
  async SAdd(key, ...members) {
    try {
      await global.redisClient.sadd(key, ...members);
    } catch (error) {
      console.log("SAdd失败", error);
    }
  },
};

module.exports = Redis;
